﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace ReferenceSwap
{
    /// <summary>
    /// Basic description of a file included in a project - abstract
    /// </summary>
    /// <remarks>
    /// inheriting from reference since it still gives the basic data we need
    /// even though it's not technically a "reference"
    /// </remarks>
    public abstract class FileReference : Reference
    {
        /// <summary>
        /// Create an instance of FileReference
        /// </summary>
        /// <param name="type">type of file</param>
        protected FileReference(FileReferenceType type)
        {
            Type = type;
        }
       
        /// <summary>
        /// Gets or Sets (Protected) if the File is a link
        /// </summary>
        public bool IsLinked
        {
            get { return !string.IsNullOrEmpty(LinkPath); }
        }
        
        /// <summary>
        /// Gets or Sets the path to the actual file relative to the link location
        /// </summary>
        public string LinkPath { get; set; }
        
        /// <summary>
        /// Gets the type of file reference
        /// </summary>
        public FileReferenceType Type { get; private set; }

        /// <summary>
        /// Load a File Reference from an Xml Node
        /// </summary>
        /// <param name="node">node to parse</param>
        /// <returns>parsed file reference</returns>
        /// <exception cref="NotSupportedException" />
        public static FileReference LoadFromXml(XmlElement node)
        {
            Guard.Against<ArgumentNullException>(node == null, "node");

            FileReferenceType type = (FileReferenceType)Enum.Parse(typeof(FileReferenceType), node.Name, true);

            if(type == FileReferenceType.Compiled)
                throw new NotSupportedException("Only Content and None files are not currently supported.");

            ContentFile content = new ContentFile(type, node.Attributes["Include"].Value);
            foreach (XmlElement child in node.ChildNodes)
            {
                if (child.Name == "Link")
                    content.LinkPath = child.InnerText;
                else if (child.Name == "CopyToOutputDirectory")
                {
                    try
                    {
                        content.Copy = (FileCopySetting)Enum.Parse(typeof(FileCopySetting), child.InnerText, true);
                    }
                    catch { } // ignore
                }
            }

            return content;
        }
    }    
}
